package com.shuai.jit;

import com.shuai.jit.entity.course.Good;
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.common.FastByIDMap;
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator;
import org.apache.mahout.cf.taste.impl.model.GenericDataModel;
import org.apache.mahout.cf.taste.impl.model.GenericUserPreferenceArray;
import org.apache.mahout.cf.taste.impl.model.file.*;
import org.apache.mahout.cf.taste.impl.neighborhood.*;
import org.apache.mahout.cf.taste.impl.recommender.*;
import org.apache.mahout.cf.taste.impl.similarity.*;
import org.apache.mahout.cf.taste.model.*;
import org.apache.mahout.cf.taste.recommender.*;
import org.apache.mahout.cf.taste.similarity.*;

import java.io.*;
import java.util.*;
//第一列：userid；第二列：itemid；第三列评分。
//1,101,5.0
// 1,102,3.0
// 1,103,2.5
// 2,101,2.0
// 2,102,2.5
// 2,103,5.0
// 2,104,2.0
// 3,101,2.5
// 3,104,4.0
// 3,105,4.5
// 3,107,5.0
// 4,101,5.0
// 4,103,3.0
// 4,104,4.5
// 4,106,4.0
// 5,101,4.0
// 5,102,3.0
// 5,103,2.0
// 5,104,4.0
// 5,105,3.5
// 5,106,4.0
public class UserCF {

    final static int NEIGHBORHOOD_NUM = 2;//临近的用户个数
    final static int RECOMMENDER_NUM = 8;//推荐物品的最大个数

    public static void main(String[] args) throws IOException, TasteException {
        String file = "src/data/testCF.csv";

        Good good=new Good();

        FastByIDMap<PreferenceArray> preMap = new FastByIDMap<PreferenceArray>();



        int temp=0;
        for(int i=0;i<10;i++){
            //构造用户1的偏好
            PreferenceArray user1 = new GenericUserPreferenceArray(100);
            for(int j=0;j<100;j++){
                user1.setUserID(j,i);
                user1.setItemID(j,new Random().nextLong()%100);
                user1.setValue(j,new Random().nextLong()%10);
            }
            preMap.put(i,user1);
        }

        DataModel model = new GenericDataModel(preMap);

//        DataModel model = new FileDataModel(new File(file));//数据模型

        // 欧几里得相似度
//        UserSimilarity user = new EuclideanDistanceSimilarity(model);//用户相识度算法

        // 皮尔深算法
        PearsonCorrelationSimilarity user=new PearsonCorrelationSimilarity(model);

        NearestNUserNeighborhood neighbor = new NearestNUserNeighborhood(2, user, model);
        //用户近邻算法
        Recommender r = new GenericUserBasedRecommender(model, neighbor, user);//用户推荐算法
        LongPrimitiveIterator iter = model.getUserIDs();///得到用户ID

        while (iter.hasNext()) {
            long uid = iter.nextLong();
            List<RecommendedItem> list = r.recommend(uid, RECOMMENDER_NUM);

            System.out.printf("uid:%s\n", uid);
            for (RecommendedItem ritem : list) {
                System.out.printf("物品的id-"+ritem.getItemID()+"物品的价值--"+ritem.getValue()+"\n");
            }
            System.out.println();
        }
    }
}